<template>
  <div class="goodsinfo-container">
    <!-- 小球的隐藏和显示 -->
    <transition @before-enter="beforeEnter" @enter="Enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{ goodsInfo.marker_price }}</del>&nbsp;&nbsp;&nbsp;
            销售价：
            <span class="now_price">￥{{goodsInfo.sell_price}}</span>
          </p>
          <p>
            购买数量：
            <numbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfo.goods_no}}</p>
          <p>库存情况：{{goodsInfo.stock_quantity}}件</p>
          <p>上架时间：{{goodsInfo.add_time | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/goodsinfo-numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id /* 将路由参数对象中的 id 挂载到data上， */,
      lunbotu: [],
      goodsInfo: {}, // 存放商品信息
      ballFlag: false /* 控制小球隐藏和显示的 */,
      selectedCount: 1 /* 用户选中的商品数量，默认为 1 */
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsInfo();
  },
  methods: {
    getLunbotu() {
      this.$http.get("getthumimages/" + this.id).then(res => {
        var res = res.body;
        if (res.status === 0) {
          res.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotu = res.message;
        }
      });
    },
    getGoodsInfo() {
      // 获取商品的信息
      this.$http.get("goods/getinfo/" + this.id).then(res => {
        var res = res.body;
        if (res.status === 0) {
          this.goodsInfo = res.message[0];
        }
      });
    },
    goDesc(id) {
      /* 使用编程式导航跳转到 图文介绍 */
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    goComment(id) {
      /* 使用编程式导航跳转到 商品评论 */
      this.$router.push({ name: "GoodsComment", params: { id } });
    },
    addToShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      // 存储商品对象：{ id:id,count:数量,price:单价,selected:false 是否选中}
      // 拼接一个要保存到 store 中 car 数组里面的 商品信息对象
      let goodsInfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsInfo.sell_price,
        selected: true
      };

      this.$store.commit('addToCar', goodsInfo);// 调用 store 中的添加商品到购物车的方法
    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    Enter(el, done) {
      el.offsetWidth;

      // 小球动画优化思路：
      // 1.先分析导致 动画 不准确的 本质原因，是因为我们把小球的横纵位移坐标给写死了
      // 2.所以我们应该去动态计算小球的横纵坐标值
      // 3.解决思路：先得到徽标的坐标，在得到小球的坐标，然后让坐标值求差，得到的就是要位移的距离
      // 如何获取徽标和小球的位置：domObject.getBoundingClientRect()

      // 获取小球的坐标位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取徽标的坐标位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 1s cubic-bezier(.4, -0.3, 1, .68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      /* 把子组件传过来的数量绑定到自己的数据中 */
      this.selectedCount = count;
      console.log("子组件传过来的值：" + this.selectedCount);
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;

  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }

  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 408px;
    left: 150px;
  }
}
</style>